Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to enable type printing #1209

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

only-cliches
Copy link

Hello,

I have a use case where I'd like to be able to export a string containing the Typescript types for a given Superstruct.

This kinda works, many of the types have a schema property in the Struct class that can be inspected to print the internal types.

However, there are quite a few types where this just isn't possible at the moment. For example: map, set, and instance types do not pass the nested type into the schema property of the Struct class, so this data isn't available after the type is created.

Additionally, type modifiers like optional, nullable, and default do not pass any information onto the Struct class, making it impossible to inspect the presence of these modifiers.

To give you an idea of what I'm trying to accomplish, here is the current code that is only partially working:

const printTS = (struct: Struct): string => {
    switch (struct.type) {
        case "array":
            return `(${printTS(struct.schema as any)})[]`
        case "enums":
            return "[" + (struct.schema as any[]).map(printTS).join(", ") + "]";
        case "union":
            return (struct.schema as any[]).map(printTS).join(" | ")
        case "literal":
            return `${struct.schema} as const`
        case "integer":
            return "number";
        case "func":
            return "(...args: any) => any"
        case "date":
            return "Date";
        case "bigint":
            return "BigInt";
        case "regexp":
            return "RegExp";
        case "number":
        case "any":
        case "never":
        case "boolean":
        case "string":
        case "unknown":
            return struct.type;
        case "object":
        case "type":
            return "{" +  Object.keys(struct.schema as any).map(s => `${s}: ${printTS((struct.schema as any)[s])}`).join(",") + "}";
        case "map": // impossible
        case "intersection": // impossible
        case "set": // impossible
        case "tuple": // impossible
        case "instance": // impossible
        break;
    }
    return ""
}

This pull request makes the minimal necessary changes to complete the function above and get it working for all types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant